﻿/*
 * Test classes for SSAS Partition Manager 
 * https://ssaspartitionmanager.codeplex.com/
 * 
 * Written by Dr. John Tunnicliffe of Decision Analytics, May 2015
 * eMail: john@decision-analytics.co.uk 
 * http://www.decision-analytics.co.uk/
 * https://www.linkedin.com/in/drjohntunnicliffe
 * 
 * Tests written with FluentAssertions, XUnit and Moq which can be installed from Nuget.  
 * 
 * FluentAssertions
 * http://www.fluentassertions.com/ 
 * 
 * XUnit
 * http://xunit.github.io/
 * 
 * Moq
 * https://github.com/Moq/moq4
 */

namespace AmoExtensions.IntegrationTests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Dynamic;
    using AmoExtensions;
    using FluentAssertions;
    using Xunit;
    using Moq;
    using log4net;

    /// <summary>
    /// Define the PartitionManagerTabularCollection fixture
    /// </summary>
    [CollectionDefinition("PartitionManagerTabularCollection")]
    public class PartitionManagerTabularCollection : ICollectionFixture<PartitionManagerTabularFixture>
    { }

    /// <summary>
    /// Test the PartitionManager class does what we expect with a real tabular model
    /// </summary>
    [Collection("PartitionManagerTabularCollection")]
    public class IntegrationTestsPartitionManagerTabular
    {
        /*
         * Here we test if the Partition class does what we expect with a real tabular model.
         * 
         * We deploy the modified AdventureWorks sample OLAP database using C# to the 
         * local server and then check the functionality of the Partition class.
         * 
         * Note that we mock up the SQL relational interaction by creating the 
         * partition objects by hand.  This gives us better flexibility :-)
         */
        #region Constructor
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public PartitionManagerTabularFixture Fixture { get; set; }

        public IntegrationTestsPartitionManagerTabular(PartitionManagerTabularFixture fixture)
        {
            Fixture = fixture;
        }
        #endregion

        [Fact]
        public void PartitionManagerTabular_ShoundNotThrow()
        {
            // we lump everything together as we want the tests to run in sequence as they are dependent upon one another
            FixtureHelperFunctions.QueryDynamic("truncate table spm.EventLog");

            Fixture.partitionManager.ListOfPartitions.Count().Should().Be(72);

            Action action = () => Fixture.partitionManager.IsPartitionListValid();
            action.ShouldNotThrow();

            Fixture.partitionManager.ListOfPartitions.Where(x => x.IsValidPartitionDefinition == true).Count().Should().Be(67);

            action = () => Fixture.partitionManager.CreatePartitions();
            action.ShouldNotThrow();

            action = () => Fixture.partitionManager.DeletePartitions();
            action.ShouldNotThrow();
        
            action = () => Fixture.partitionManager.UpdateCubeMetaData();
            action.ShouldNotThrow();

            // now check we have four rows in the EventLog
            string sql = "select LogTime, Thread, LogLevel, Logger, LogMessage from spm.EventLog";
            IEnumerable<dynamic> LogEvents = FixtureHelperFunctions.QueryDynamic(sql);
            LogEvents.Where(x => x.LogLevel == "ERROR").Count().Should().Be(5);
            LogEvents.Where(x => x.LogLevel == "WARN").Count().Should().Be(1);
            LogEvents.Where(x => x.LogLevel == "INFO").Count().Should().Be(68);
        }
    }
}
